package com.wanmait.recycle.controller;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.servlet.ModelAndView;

import com.github.pagehelper.PageInfo;
import com.wanmait.recycle.pojo.Firsttype;
import com.wanmait.recycle.pojo.Photolib;
import com.wanmait.recycle.pojo.Rubbish;
import com.wanmait.recycle.pojo.Secondtype;
import com.wanmait.recycle.service.FirsttypeService;
import com.wanmait.recycle.service.PhotolibService;
import com.wanmait.recycle.service.RubbishService;
import com.wanmait.recycle.service.SecondtypeService;
import com.wanmait.recycle.util.UploadPhotosUtil;

@Controller
@RequestMapping("/manager")
public class PhotoLibController {

	@Resource
	private PhotolibService photolibService;
	@Resource
	private FirsttypeService firsttypeService;
	@Resource
	private SecondtypeService secondtypeService;
	@Resource
	private RubbishService rubbishService;
	
	/*获得photo列表*/
	@RequestMapping("rubbish/rubbishPicShow")
	public ModelAndView rubbishPicShow(String beginTime,String endTime,String searchStr) {
		ModelAndView mv = new ModelAndView();
		PageInfo<Photolib> pageInfo = null;
		Map<String,String> criteriaMap = new HashMap<>();
		
		if(beginTime!=""||endTime!=""||searchStr!="") {
			pageInfo = photolibService.findByCriteria(beginTime, endTime, searchStr);
			criteriaMap.put("beginTime", beginTime);
			criteriaMap.put("endTime", endTime);
			criteriaMap.put("searchStr", searchStr);
			
		}else {
			pageInfo = photolibService.findAllPhotolib();
		}
		mv.addObject("pageInfo", pageInfo);
		mv.addObject("criteriaMap", criteriaMap);
		return  mv;
	}
	/*根据Id删除图片*/
	@RequestMapping("/deleteById")
	@ResponseBody
	public void deleteById(Integer photoId) {
		photolibService.delete(photoId);
	}
	/*删除多张图片*/
	@RequestMapping("/deleteByIds")
	@ResponseBody
	public void deleteByIds(String photoIds) {
		photolibService.deleteByIds(photoIds);
	}
	/*添加图片并获得一级分类*/
	@RequestMapping("rubbish/addRubbishPhoto")
	@ModelAttribute("firstTypes")
	public List<Firsttype> addRubbishPhoto() {
		return firsttypeService.findAll();
		
	}
	/*获得二级分类*/
	@RequestMapping("/getSecondTypes")
	@ResponseBody
	public List<Secondtype> getSecondType(Integer firstTypeId){
	
		List<Secondtype> secondTypes = secondtypeService.findAllByFirsttypeId(firstTypeId);
		return secondTypes;
	}
	/*获得rubbishNames*/
	@RequestMapping("/getRubbishNames")
	@ResponseBody
	public List<Rubbish> getRubbishNames(Integer secondTypeId){
	
		List<Rubbish> rubbish = rubbishService.findAllBySecondTypeId(secondTypeId);
		return rubbish;
	}
	/*图片提交*/
	@RequestMapping("/addPhotos")
	public String addPhotos(Integer rubbishId,@RequestParam(value="file",required=false) MultipartFile[] file,HttpServletRequest request)throws Exception {
		
		List<String> photoNames = new ArrayList();
		
		for (MultipartFile mf : file) {
			String photoName = UploadPhotosUtil.getPhotoNameAndSave("/static/rubbish_img", mf, request);
			photoNames.add(photoName);
		}
		photolibService.insertPhotos(photoNames, rubbishId);
		return "redirect:/manager/rubbish/rubbishPicShow";
	}
		

	
	
	
	
	
	
	
	
	
	
	
}
